package com.dm.shop.vo;

import com.dm.shop.common.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @desc:
 * @date: 2021/7/20 19:30
 */

@Data //lombok注解
@ApiModel(value = "封装返回的对象",description = "") //Swagger用户返回对象类型上
@NoArgsConstructor //为Result类提供一个无参的构造方法
public class Result {
    //swgger配置用在出入参数配置
    @ApiModelProperty(value = "返回code,400表示失败，200为成功")
    private int code = 200;

    @ApiModelProperty(value = "返回提示信息")
    private String msg = "请求成功";

    @ApiModelProperty(value = "返回数据")
    private Object data = null;

    //有参构造函数
    public Result(int code, String msg, Object data)
    {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 请求失败统一用枚举类型返回
     * @param resultEnum
     */
    public Result(ResultEnum resultEnum)
    {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    /**
     * 自定义消息构造函数
     * @param msg
     * @param data
     */
    public Result(String msg, Object data)
    {
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回信息
     * @param msg
     */
    public Result(String msg)
    {
        this.msg = msg;
    }

    /**
     * 只返回数据，消息用默认值
     * @param data
     */
    public Result(Object data)
    {
        this.data = data;
    }
}
